import type { ComponentRecordType, GenerateMenuAndRoutesOptions } from '@croco/framework/types';

import { generateAccessible } from '@croco/framework/access';
import { preferences } from '@croco/framework/preferences';

import { ElMessage } from 'element-plus';

import { getUserMenus } from '#/api';
import { BasicLayout, IFrameView } from '#/layouts';
import { $t } from '#/locales';

const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');

async function generateAccess(options: GenerateMenuAndRoutesOptions) {
    const pageMap: ComponentRecordType = import.meta.glob('../views/**/*.vue');

    const layoutMap: ComponentRecordType = {
        BasicLayout,
        IFrameView,
    };

    return await generateAccessible(preferences.app.accessMode, {
        ...options,
        fetchMenuListAsync: async () => {
            ElMessage({
                duration: 1500,
                message: `${$t('common.loadingMenu')}...`,
            });
            return await getUserMenus();
        },
        // 可以指定没有权限跳转403页面
        forbiddenComponent,
        // 如果 route.meta.menuVisibleWithForbidden = true
        layoutMap,
        pageMap,
    });
}

export { generateAccess };
